package com.itheima.day16反射机制与类加载.反射核心案例_使用场景;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;

public class 类加载加反射机制案例5 {
    public static void main(String[] args) throws Exception {
        //1.类加载配置文件
        InputStream resourceAsStream = 类加载加反射机制案例5.class.getClassLoader().getResourceAsStream("prop.properties");
        //2.转换字符缓冲流
        BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
        //3.场景prop对象
        Properties prop = new Properties();
        //4.加载字符缓冲流
        prop.load(br);
        //5.获取类对象
        Class className = Class.forName(prop.getProperty("className"));
        //6.获取构造方法
        Constructor con = className.getDeclaredConstructor();
        //7.创建对象,暴力反射
        con.setAccessible(true);
        Object obj = con.newInstance();
        //8.获得方法对象
        Method method = className.getDeclaredMethod(prop.getProperty("method"));
        //9.对象执行方法
        method.setAccessible(true);
        Object invoke = method.invoke(obj);
        if (invoke != null) {
            System.out.println(invoke);
        }
    }
}
